﻿using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Filters;

namespace SmartCity.Shared
{
    /// <summary>
    /// 异常过滤器
    /// </summary>
    public class ExceptionFilter : ExceptionFilterAttribute
    {
        public override void OnException(ExceptionContext context)
        {
            context.Result = JsonResultFactory.Error(context.Exception);
            context.ExceptionHandled = true;
        }

        public override async Task OnExceptionAsync(ExceptionContext context)
        {
            await Task.Run(() =>
            {
                context.Result = JsonResultFactory.Error(context.Exception);
                context.ExceptionHandled = true;
            });
        }
    }
}